package com.simon.hse.activity.danger;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.CommonInput;
import com.simon.hse.http.entity.DangerApprovalOutput;
import com.simon.hse.interfaces.PopListener;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;


import retrofit2.Call;

/**
 * 隐患审批
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_danger_approval)
public class DangerApprovalActivity extends BaseActivity implements PopListener {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.smartrefresh)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private int currentPage = 1, totalPage = 1;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<DangerApprovalOutput.DataBean.PageDataBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("隐患确认");

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left, R.id.iv_common_right})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.iv_common_right:
                Intent intent = new Intent(DangerApprovalActivity.this, DangerReportDetailActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void initRecyclerView() {
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage*10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(DangerApprovalActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(DangerApprovalActivity.this, R.layout.layout_item_danger_approval, mDataBeanList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<DangerApprovalOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, DangerApprovalOutput.DataBean.PageDataBean dataBean) {
                TextView tv_danger_approval_title = holder.getView(R.id.tv_danger_approval_title);
                TextView tv_danger_approval_person = holder.getView(R.id.tv_danger_approval_person);
                TextView tv_danger_approval_date = holder.getView(R.id.tv_danger_approval_date);

                tv_danger_approval_title.setText(dataBean.getRiskDescription());
                tv_danger_approval_person.setText(dataBean.getDiscovererText());
                String discoveryTime = dataBean.getDiscoveryTime();
                if (discoveryTime.length() > 16) {
                    discoveryTime = discoveryTime.substring(0, 16);
                }
                tv_danger_approval_date.setText(discoveryTime);

            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {
                initData();
            }

            @Override
            public void setOnItemClickListener(View view, int position) {
                Intent intent = new Intent(DangerApprovalActivity.this, DangerApprovalDetailActivity.class);
                DangerApprovalOutput.DataBean.PageDataBean pageDataBean = mDataBeanList.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("ID", pageDataBean.getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        CommonInput commonInput = new CommonInput();
        CommonInput.ModelBean modelBean = new CommonInput.ModelBean();
        commonInput.setModel(modelBean);
        commonInput.setLimit(10);
        commonInput.setOffset(10 * (currentPage - 1));

        Call<String> call = HttpManager.getInstance().requestService().DANGER_APPROVAL_LIST(HttpManager.getInstance().requestBody(commonInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                DangerApprovalOutput dangerApprovalOutput = new Gson().fromJson(result, DangerApprovalOutput.class);
                if (dangerApprovalOutput.getResult().equals("Success")) {
                    totalPage = dangerApprovalOutput.getData().getRowCount();
                    List<DangerApprovalOutput.DataBean.PageDataBean> dataBeanList = dangerApprovalOutput.getData().getPageData();
                    mDataBeanList.addAll(dataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();

                    mSmartRefresh.finishRefresh();
                    mSmartRefresh.finishLoadMore();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(dangerApprovalOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    @Override
    public void upData() {
        currentPage = 1;
        mDataBeanList.clear();
        initData();
    }
}
